/**
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// handle ecma.callithisrangedyn
// regs set as follow
// x9 - frame.vregs, x10 - insn_ptr, x12 - method, x15 - acc, x13, x14 - temp

    // w15 <- num_args
    ldrh w15, [x10, 0]
    add w15, w15, (3 - 1) // described in js_decode_call_instr.h

    // x9 <- range in frame.vregs
    ldrb w13, [x10, 2]
    add x9, x9, x13, lsl 3

    // ABI arg reg 0 <- ark::Method*
    // ABI arg reg 1 <- num_args
    mov x0, x12
    mov w1, w15

    // Reserve stack args
    // x12 <- stack pointer
    sub x12, sp, x15, lsl 3
    and x12, x12, -16
    mov sp, x12

    // stack arg0 <- func (range0)
    // stack arg1 <- new_target (undefined)
    // stack arg3 <- this (range1)
    ldr x13, [x9,  0 * FRAME_VREGISTER_SIZE]
    str x13, [x12, 0 * 8]
    mov x13, TAGGED_VALUE_UNDEFINED
    str x13, [x12, 1 * 8]
    ldr x13, [x9,  1 * FRAME_VREGISTER_SIZE]
    str x13, [x12, 2 * 8]

    subs w15, w15, 3
    beq .Linvoke

    add x9, x9, 1 * FRAME_VREGISTER_SIZE
    add x12, x12, 2 * 8
    lsl w15, w15, 3
1:
    ldr x13, [x9, x15]
    str x13, [x12, x15]
    subs w15, w15, FRAME_VREGISTER_SIZE
    bhi 1b

    b .Linvoke
